package Testclass;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/cookie")
public class Cookie1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建一个名为“username”的Cooike
        Cookie usernamecookie = new Cookie("username", "john_doe");
        //设置Cookie的过期时间
        usernamecookie.setMaxAge(10);
        //设置Cookie的路径
        //usernamecookie.setPath("/");
        //发送Cookie给客户端
        resp.addCookie(usernamecookie);
    }
}
